<template>
	<view class="">
		<el-form :model="form" :rules="rules" ref="form" label-width="100px" class="form">
			<el-form-item label="账号" prop="account">
				<el-input v-model="form.account"></el-input>
			</el-form-item>
			<el-form-item label="密码"  prop="password">
				<el-input type="password" v-model="form.password"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('form')">确定</el-button>
				<el-button @click="resetForm('form')">重置</el-button>
			</el-form-item>
		</el-form>
			<view class="btn">
        <view class="tip" @tap="toRegister">
		    没有账号？点击此处注册账号
	    </view>
    </view>
 </view>
</template>

<script>
export default {
  name: "login",
  components: {},
	data(){
		return{
			form:{
			 account:"",
				password:"",
			},
			rules: {
				account: [
					{ required: true, message: '请输入账号', trigger: 'blur' },
				],
				password: [
					{ required: true, message: '请输入密码', trigger: 'blur' },
				],
			}
		}
	},
	methods:{
			submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const data={
							account:this.form.account,
							password:this.form.password,
						}
						console.log('data',data)
						uni.showLoading({
							title: '提交中',
							mask: true
						});
						this.$Ajax('login', data, res => {
							uni.hideLoading();
							if (res.code == 200) {
								// uni.setStorageSync('userId', res.data._id);
								this.getUserInfo(res.data._id)
							} else {
								this.$tools.toast(res.message)
							}
						},err=>{
							uni.hideLoading();
							console.log('err',err)
						})
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
			getUserInfo(userId){
				const data={
					userId:userId
				}
				this.$Ajax('getUserInfo',data,res=>{
					// console.log(res.data)
					uni.setStorageSync('userInfo', res.data);
					this.$tools.toastJump('登录成功','/pages/index/index')
				},err=>{
					console.log('err',err)
				})
			},
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
			toRegister(){
				this.$tools.jump('./reg')
			}
	}
};
</script>

<style>
uni-input{
	margin-top: 10px;
}
.tip{
	text-align: center;
	cursor: pointer;
}
.form{
  width: 375px;
  max-width: 80%;
  margin: 20px auto;
}
</style>
